
import 'package:flutter/material.dart';
import '../../../common/style/extension_util.dart';

class ItemSelectWidget extends StatefulWidget {
   List<dynamic>? selDataSource = [];
  final List<dynamic> dataSource;
  final Function(List<dynamic> selData)? callBack;
    ItemSelectWidget({super.key, required this.dataSource,this.selDataSource ,this.callBack});
  @override
  State<ItemSelectWidget> createState() => _ItemSelectWidgetState();
}

class _ItemSelectWidgetState extends State<ItemSelectWidget> {
  @override
  Widget build(BuildContext context) {

    return ListView.separated(separatorBuilder: (_,index){
      return const Divider(color: Color(0xFFE6E6E6),thickness: 1,indent: 12,endIndent: 12,);
    },itemBuilder: (_,index){
      var maxIndex = (widget.selDataSource?.length ?? 0) - 1;
      var data = widget.dataSource[index];
      var name = "";
      if(data is LabelValue){
        name = data.label;
      }else if(data is String){
        name = data;
      }
      bool isSel = false;
        for(var item in widget.selDataSource!){
          if(item ==  widget.dataSource[index]){
            isSel = true;
          }
        }
      return
        Container(
            color: Colors.white,
            height: 44,
            padding: const EdgeInsets.symmetric(horizontal: 12),
            child:
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
        Text(name),
        const Icon(Icons.check,color: Colors.green,).addVisibility(isSel)
      ],)).addGestureTap(() {
          if(isSel){
            widget.selDataSource!.remove(data);
          }else{
            widget.selDataSource!.add(data);
          }
          setState(() {

          });
          widget.callBack?.call(widget.selDataSource!);
        });
    },itemCount: widget.dataSource.length,).addContainer();
  }
}

